home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / MPW Oberon 2.1168 / OExamples / SillyBalls.mod < prev    next >
Encoding:
Text File  |  1995-07-03  |  8.8 KB  |  246 lines  |  [TEXT/MPS ]

  1. (*------------------------------------------------------------------------------
  2. #
  3. #    Macintosh Developer Technical Support
  4. #
  5. #    Simple Color QuickDraw Sample Application
  6. #
  7. #    SillyBalls
  8. #
  9. #    SillyBalls.p    -    Pascal Source
  10. #
  11. #    Copyright © 1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                    8/88
  15. #
  16. #    Components:    SillyBalls.p        August 1, 1988
  17. #                SillyBalls.make        August 1, 1988
  18. #
  19. #    This is a very simple sample program that demonstrates how to use Color 
  20. #    QuickDraw.  It is about two pages of code, and does nothing more than open
  21. #    a color window and draw randomly colored ovals in the window.
  22. #    
  23. #    The purpose is to show how to get some initial results with Color QuickDraw.
  24. #    It is a complete program and is very short to be as clear as possible.
  25. #    
  26. #    It does not have an Event Loop.  It is not fully functional in the sense that
  27. #    it does not do all the things you would expect a well behaved Macintosh 
  28. #    program to do, like size the window naturally, have an event loop, use menus, 
  29. #    etc.
  30. #
  31. #    See Sample and TESample for the general structure and MultiFinder techniques that
  32. #    we recommend that you use when building a new application.
  33. #
  34. ------------------------------------------------------------------------------*)
  35.  
  36. MODULE SillyBalls;
  37. (*
  38.     Version 1.0: 6/2/88
  39.     
  40.     purpose        To demonstrate a simple color App using Color QuickDraw.
  41.                         It draws colored balls in a color window, then uses colored
  42.                         text inverted in the ball.  The ball location and color is Random.
  43.                         
  44.                         This program was written by Bo3b Johnson, 1/88.
  45.                         
  46.                         The inverted Bob text was a Skippy Blair special concept,
  47.                         kept for obvious aesthetic reasons.
  48.                         
  49.                         You can build the program with this junk:
  50. pascal SillyBalls.p
  51. Link SillyBalls.p.o ∂
  52.     "{MPW}Libraries:"Interface.o ∂
  53.     "{MPW}Libraries:"Runtime.o ∂
  54.     "{MPW}PLibraries:"Paslib.o ∂
  55.     -o SillyBalls
  56. SillyBalls
  57. *)
  58.  
  59. (* Where does it fit:
  60.     This is a series of sample programs for those doing development
  61.     using Color QuickDraw.  Since the whole color problem depends
  62.     upon the exact effect desired, there are a number of answers
  63.     to how to use colors, from the simple to the radically complex.
  64.     These programs try to cover the gamut, so you should use 
  65.     which ever seems appropriate.  In most cases, use the simplest
  66.     one that will give the desired results.  The compatibility
  67.     rating is from 0..9 where low is better.  The more known risks 
  68.     there are the higher the rating.
  69.     
  70.     
  71.     The programs (in order of compatibility):
  72.     
  73.         SillyBalls:    (***)
  74.             This is the simplest use of Color QuickDraw, and does
  75.             not use the Palette Manager.  It draws randomly colored
  76.             balls in a color window.  This is intended to give you
  77.             the absolute minimum required to get color on the screen.
  78.             Written in straight Pascal code.
  79.             Compatibility rating = 0, no known risks.
  80.         
  81.         FracAppPalette:
  82.             This is a version of FracApp that uses only the Palette
  83.             Manager.  It does not support color table animation
  84.             since that part of the Palette Manager is not sufficient.
  85.             The program demonstrates a full color palette that is
  86.             used to display the Mandelbrot set.  It uses an offscreen
  87.             gDevice w/ Port to handle the data, using CopyBits to
  88.             draw to the window.  The Palette is automatically 
  89.             associated with each window.  The PICT files are read
  90.             and written using the bottlenecks, to save on memory
  91.             useage.
  92.             Written in MacApp Object Pascal code.
  93.             Compatibility rating = 0, no known risks.
  94.         
  95.         TubeTest:
  96.             This is a small demo program that demonstrates using the
  97.             Palette Manager for color table animation.  It uses a 
  98.             color palette with animating entries, and draws using the
  99.             Palette Manager.  There are two circles of animating colors
  100.             which gives a flowing tube effect.  This is a valid case
  101.             for using the animating colors aspect of the Palette Manager,
  102.             since the image is being drawn directly.
  103.             Written in straight Pascal code.
  104.             Compatibility rating = 0, no known risks.
  105.         
  106.         FracApp:
  107.             This is the ‘commercial quality’ version of FracApp.  This
  108.             version supports color table animation, using an offscreen
  109.             gDevice w/ Port, and handles multiple documents.  The
  110.             CopyBits updates to the screen are as fast as possible.  The
  111.             program does not use the Palette Manager, except to
  112.             provide for the system palette, or color modes with less than
  113.             255 colors.  For color table animation using an offscreen
  114.             gDevice w/ Port, it uses the Color Manager and handles the
  115.             colors itself.  Strict compatibility was relaxed to allow for
  116.             a higher performance program.  This is the most ‘real’ of the
  117.             sample programs.
  118.             Written in MacApp Object Pascal code.
  119.             Compatibility rating = 2.  (nothing will break, but it may not
  120.             always look correct.)
  121.         
  122.         FracApp300:
  123.             This doesn't support colors, but demonstrates how to create and
  124.             use a 300 dpi bitmap w/ Port.  The bitmap is printed at full
  125.             resolution on LaserWriters, and clipped on other printers (but
  126.             they still print).  It demonstrates how to use a high resolution
  127.             image as a PICT file, and how to print them out.
  128.             Written in MacApp Object Pascal code.
  129.             Compatibility rating = 1.  (The use of PrGeneral is slightly 
  130.             out of the ordinary, although supported.)
  131. *)
  132.  
  133. IMPORT SYSTEM, Types, Quickdraw, Windows, Events, OSUtils, SegLoad,
  134.        Fonts, Menus, TextEdit, Dialogs, QuickdrawText;
  135.  
  136. CONST
  137.     BallWidth    = 20;
  138.     BallHeight    = 20;
  139.     BobSize        = 8;        (* Size of text in each ball. *)
  140.  
  141. VAR
  142.     windRect    : Types.Rect;
  143.     
  144.     
  145. (* Initialize everything for the program, make sure we can run. *)
  146.  
  147. PROCEDURE Initialize;
  148.  
  149. VAR
  150.     mainPtr        : Windows.WindowPtr;
  151.     error        : Types.OSErr;
  152.     theWorld    : OSUtils.SysEnvRec;
  153.     
  154. BEGIN
  155.     (* Test the computer to be sure we can do color.  If not we would crash, which
  156.     would be bad.  If we can’t run, just beep and exit. *)
  157.     error := OSUtils.SysEnvirons(1, theWorld);
  158.     IF ¬theWorld.hasColorQD THEN
  159.         OSUtils.SysBeep (50);
  160.         SegLoad.ExitToShell;                        (* If no color QD, we must leave. *)
  161.     END;
  162.     
  163.     (* Initialize all the needed managers. *)
  164.     Quickdraw.InitGraf(Quickdraw.thePort);
  165.     Fonts.InitFonts;
  166.     Windows.InitWindows;
  167.     Menus.InitMenus;
  168.     TextEdit.TEInit;
  169.     Dialogs.InitDialogs(NIL);
  170.     Quickdraw.InitCursor;
  171.  
  172.     (* To make the Random sequences truly random, we need to make the seed start
  173.     at a different number.  An easy way to do this is to put the current time
  174.     and date into the seed.  Since it is always incrementing the starting seed
  175.     will always be different.  Don’t for each call of Random, or the sequence
  176.     will no longer be random.  Only needed once, here in the init. *)
  177.     OSUtils.GetDateTime (Quickdraw.randSeed);
  178.  
  179.     (* Make a new window for drawing in, and it must be a color window.  The window is
  180.     full screen size, made smaller to make it more visible. *)
  181.     windRect := Quickdraw.screenBits.bounds;
  182.     Quickdraw.InsetRect (windRect, 50, 50);
  183.     mainPtr := Windows.NewCWindow(NIL, windRect, 'Bo3b Land', TRUE, Windows.documentProc, 
  184.                             Windows.WindowPtr(-1), FALSE, 0);
  185.         
  186.     Quickdraw.SetPort(mainPtr);                        (* set window to current graf port   *)
  187.     QuickdrawText.TextSize(BobSize);                        (* smaller font for drawing. *)
  188. END Initialize;
  189.  
  190.  
  191. (* NewBall: make another ball in the window at a random location and color. *)
  192.  
  193. PROCEDURE NewBall;
  194.  
  195. VAR
  196.     ballColor    : Quickdraw.RGBColor;
  197.     ballRect    : Types.Rect;
  198.     newLeft,
  199.     newTop        : LONGINT;
  200.     
  201. BEGIN
  202.     (* Make a random new color for the ball. *)
  203.     ballColor.red := Quickdraw.Random();
  204.     ballColor.green := Quickdraw.Random();
  205.     ballColor.blue := Quickdraw.Random();
  206.     
  207.     (* Set that color as the new color to use in drawing. *)
  208.     Quickdraw.RGBForeColor (ballColor);
  209.  
  210.     (* Make a Random new location for the ball, that is normalized to the window size.  
  211.     This makes the Integer from Random into a number that is 0..windRect.bottom
  212.     and 0..windRect.right.  They are normalized so that we don't spend most of our
  213.     time drawing in places outside of the window. *)
  214.     newTop := Quickdraw.Random(); newLeft := Quickdraw.Random();
  215.     newTop := ((newTop+32767) * windRect.botRight.v) DIV 65536;
  216.     newLeft := ((newLeft+32767) * windRect.botRight.h) DIV 65536;
  217.     Quickdraw.SetRect(ballRect, SHORT(newLeft), SHORT(newTop),
  218.                       SHORT(newLeft)+BallWidth, SHORT(newTop)+BallHeight);
  219.     
  220.     (* Move pen to the new location, and paint the colored ball. *)
  221.     Quickdraw.MoveTo(SHORT(newLeft), SHORT(newTop));
  222.     Quickdraw.PaintOval (ballRect);
  223.     
  224.     (* Move the pen to the middle of the new ball position, for the text *)
  225.     Quickdraw.MoveTo(ballRect.topLeft.h + BallWidth DIV 2 - BobSize, 
  226.             ballRect.topLeft.v + BallHeight DIV 2 + BobSize DIV 2 -1);
  227.     
  228.     (* Invert the color and draw the text there.  This won’t look quite right in 1 bit
  229.     mode, since the foreground and background colors will be the same.  Color
  230.     QuickDraw special cases this to not invert the color, to avoid invisible
  231.     drawing. *)
  232.     Quickdraw.InvertColor(ballColor); 
  233.     Quickdraw.RGBForeColor(ballColor);
  234.     QuickdrawText.DrawString('Bob');
  235. END NewBall;
  236.  
  237. (*$MAIN*)
  238. BEGIN       (* Main body of program SillyBalls *)
  239.     Initialize;
  240.     
  241.     REPEAT
  242.         NewBall;
  243.     UNTIL Events.Button()
  244.     
  245. END SillyBalls.
  246.